package com.egao.common.module.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.core.Constants;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.entity.CollegeRate;
import com.egao.common.module.system.service.CollegeRateService;
import com.egao.common.module.system.service.CollegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 学院管理
 * @author lym
 */
@Api(tags = "学院管理")
@RestController
@RequestMapping("${api}")
public class CollegeController extends BaseController {

    @Autowired
    private CollegeService collegeService;
    @Autowired
    private CollegeRateService collegeRateService;

    @PreAuthorize("hasAuthority('sys:college:list')")
    @ApiOperation("查询全部学院")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "xl", value = "学历", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "学院名称或编码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "yearId", value = "毕业年份", required = false, dataType = "int", paramType = "query"),
    })
    @GetMapping("/sys/college/list")
    public JsonResult list(Integer xl, String keyword, Integer yearId) {
        List<College> collegeList = collegeService.listAll(xl, keyword);
        List<Integer> collegeIds = collegeList.stream().map(College::getId).collect(Collectors.toList());

        List<CollegeRate> collegeRateList = collegeRateService.list(new LambdaQueryWrapper<CollegeRate>()
                .eq(CollegeRate::getYearId, yearId)
                .in(CollegeRate::getCollegeId, collegeIds));

        for (College college : collegeList) {
            for (CollegeRate collegeRate : collegeRateList) {
                if (college.getId().equals(collegeRate.getCollegeId())) {
                    college.setCollegeRateId(collegeRate.getId());
                    college.setRate5(collegeRate.getRate5());
                    college.setRate8(collegeRate.getRate8());
                    break;
                }
            }
        }

        return JsonResult.ok().setData(collegeList);
    }

    @PreAuthorize("hasAuthority('sys:college:insert')")
    @OperLog(value = "学院管理", desc = "添加学院", result = true)
    @ApiOperation("添加学院")
    @PostMapping("/sys/college/insert")
    public JsonResult insert(@RequestBody College college) {
        JsonResult jsonResult = collegeService.insertCollege(college);
        if (Constants.RESULT_OK_CODE == jsonResult.getCode()) {
            CollegeRate collegeRate = new CollegeRate();
            collegeRate.setCollegeId(college.getId());
            collegeRate.setYearId(college.getYearId());
            collegeRate.setRate5(college.getRate5());
            collegeRate.setRate8(college.getRate8());
            collegeRateService.save(collegeRate);
        }
        return jsonResult;
    }

    @PreAuthorize("hasAuthority('sys:college:update')")
    @OperLog(value = "学院管理", desc = "修改学院", result = true)
    @ApiOperation("修改学院")
    @PutMapping("/sys/college/update")
    public JsonResult update(@RequestBody College college) {
        boolean b = collegeService.updateById(college);
        CollegeRate collegeRate = new CollegeRate();
        collegeRate.setId(college.getCollegeRateId());
        collegeRate.setCollegeId(college.getId());
        collegeRate.setYearId(college.getYearId());
        collegeRate.setRate5(college.getRate5());
        collegeRate.setRate8(college.getRate8());
        collegeRateService.saveOrUpdate(collegeRate);

        return b ? JsonResult.ok("修改成功") : JsonResult.error("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:major:delete')")
    @OperLog(value = "学院管理", desc = "删除学院", result = true)
    @ApiOperation("删除学院")
    @DeleteMapping("/sys/college/delete")
    public JsonResult delete(Integer id) {
        if(id == null){
            throw new ParameterException("参数对象为空");
        }
        return collegeService.removeById(id) ? JsonResult.ok("删除成功") : JsonResult.error("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:college:update')")
    @OperLog(value = "学院管理", desc = "导入目标就业率", result = true)
    @ApiOperation("导入目标就业率")
    @Transactional
    @PostMapping("/sys/college/import")
    public JsonResult importCollege(MultipartFile file, Integer yearId) {
        return collegeService.importCollege(file, yearId);
    }
}
